package com.kl.modular.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.kl.common.exception.SystemException;
import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.modular.system.dao.bo.SystemAdminRoleBo;
import com.kl.modular.system.dao.mapper.SystemAdminRoleMapper;
import com.kl.modular.system.dao.model.SystemAdminRole;
import com.kl.modular.system.service.api.ISystemAdminRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户角色serviceImpl业务实现
 * @author XRP_ZJB	
 * @date 2016-08-11 21:17
 * @(C) XIGNSOFT Copyright 
 */
@Service
public class SystemAdminRoleServiceImpl implements ISystemAdminRoleService {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemAdminRoleServiceImpl.class);
	
	@Autowired
	private SystemAdminRoleMapper systemAdminRoleMapper;
	
	/**
	 * 用户角色列表分页
	 */
	public Pager<SystemAdminRole> getListByBoPage(SystemAdminRoleBo systemAdminRoleBo, PageParameter pageParameter){
		pageParameter.setPageBoolean(true);
		List<SystemAdminRole> systemAdminRoleList = systemAdminRoleMapper.getListByBoPage(systemAdminRoleBo, pageParameter);
		return new Pager<SystemAdminRole>(pageParameter, systemAdminRoleList);
	}
	
	/**
	 * 添加用户角色
	 */
	public void addSystemAdminRole(SystemAdminRole systemAdminRole) throws Exception {
		int result = systemAdminRoleMapper.insert(systemAdminRole);
		if(result == 0){
			logger.error("添加用户角色发生错误>>data:" + JSON.toJSONString(systemAdminRole));
			throw new SystemException("添加用户角色发生错误");
		}
	}

	/**
	 * 更新用户角色
	 */
	public void updateByCode(SystemAdminRole systemAdminRole) throws Exception {
		int result = systemAdminRoleMapper.updateByPrimaryKey(systemAdminRole);
		if(result ==0){
			logger.error("更新用户角色发生错误>>data:" + JSON.toJSONString(systemAdminRole));
			throw new SystemException("更新用户角色发生错误");
		}
	}
	
	/**
	 * 删除用户角色
	 */
	public void deleteByCode(String[] codes) throws Exception {
		int result = systemAdminRoleMapper.deleteByPrimaryKey(codes);
		if(result == 0){
			logger.error("删除用户角色发生错误>>data:" + JSON.toJSONString(codes));
			throw new SystemException("删除用户角色发生错误");
		}
	}
	
	/**
	 * 查询用户角色
	 */
	public SystemAdminRole getSystemAdminRoleByCode(String code) throws Exception {
		SystemAdminRole systemAdminRole = systemAdminRoleMapper.selectByPrimaryKey(code);
		if(systemAdminRole == null){
			logger.error("查询用户角色发生错误>>data:" + JSON.toJSONString(systemAdminRole));
			throw new SystemException("未查询到用户角色数据");
		}
		return systemAdminRole;
	}

	/**
	 * 用户角色列表
	 */
	public List<SystemAdminRole> getListByBo(SystemAdminRoleBo systemAdminRoleBo) {
		PageParameter pageParameter = new PageParameter();
		pageParameter.setPageBoolean(false);
		return systemAdminRoleMapper.getListByBoPage(systemAdminRoleBo, pageParameter);
	}

	/**
	 * 逻辑删除用户角色关系
	 */
	public void updateDeleteByAdminCode(String adminCode) {
		systemAdminRoleMapper.updateDeleteByAdminCode(adminCode);
	}
	
}
